import QtQuick 1.0
import "js/TwitterAPI.js" as TwitterAPI

QMLTwitterBase{
    id: _root

    //////////////////////////////////////////////////////////
    // properties

    property string _token: ""


    //////////////////////////////////////////////////////////
    // changed

    //////////////////////////////////////////////////////////
    // signals

    // 1.response request token
    signal responseRequestToken(string url)
    // 2.response access token
    signal responseAccessToken(string token, string token_secret
                               , string screen_name, string user_id)


    //////////////////////////////////////////////////////////
    // methods

    // 1-1.request token
    // https://dev.twitter.com/docs/api/1/post/oauth/request_token
    function requestToken(){
        var accessor = {
            consumerSecret: consumerSecret
            , tokenSecret: ''
        };

        var message = {
            method: "GET",
            action: TwitterAPI.URL_APIS["URL_OAUTH_REQUEST_TOKEN"],
            parameters: {
                oauth_signature_method: "HMAC-SHA1"
                , oauth_consumer_key: consumerKey
            }
        };

        TwitterAPI.OAuth.setTimestampAndNonce(message);
        TwitterAPI.OAuth.SignatureMethod.sign(message, accessor);
        var target = TwitterAPI.OAuth.addToURL(message.action, message.parameters);

        //Request Token/Request Token Secretを取り出して、PINを取得するためのURLを作成
        requestHttp("", "GET", target , responseHttpRequestToken);
    }

    // 1-2.responseを処理
    function responseHttpRequestToken(text)
    {
        var url = "";
        var datas = text.split("&");
        if(datas.length === 0){
        }else{
            url = TwitterAPI.URL_APIS["URL_OAUTH_AUTHORIZE"] + "?" + datas[0];
            datas = datas[0].split("=");
            if(datas.length < 1){
                _token = "";
            }else{
                _token = datas[1];
            }
        }
        if(_token.length === 0){
            //fail
            responseRequestToken("");
        }else{
            //success
            responseRequestToken(url);
        }
    }

    // 2-1.access token
    // https://dev.twitter.com/docs/api/1/post/oauth/access_token
    function accessToken(pin_code){
        if(_token.length === 0){
            //NG
            //UIへのコールバック
            responseAccessToken("", "", "", "");
        }else{
            var accessor = {
                consumerSecret: consumerSecret,
                tokenSecret: ''
                };

            var message = {
                method: "GET",
                action: TwitterAPI.URL_APIS["URL_OAUTH_ACCESS_TOKEN"],
                parameters: {
                    oauth_signature_method: "HMAC-SHA1",
                    oauth_consumer_key: consumerKey,
                    oauth_token: _token, // Request Token
                    oauth_verifier: pin_code
                    }
                };
            TwitterAPI.OAuth.setTimestampAndNonce(message);
            TwitterAPI.OAuth.SignatureMethod.sign(message, accessor);
            var target = TwitterAPI.OAuth.addToURL(message.action, message.parameters);

            //認証
            requestHttp("", "GET", target, responseHttpAccessToken);
        }
    }


    //2-2.認証結果のAccess Token/Access Token Secret
    function responseHttpAccessToken(text, on_func)
    {
        var url = "";

        var pos = text.indexOf("</html>");
        if(pos > 0){
            // 前半のHTMLを切る
            text = text.substring(pos + 7);
        }else{
            // htmlない場合
        }
        var datas = text.split("&");
        if(datas.length === 0){
        }else{
            var items = null;
            var data = "";

            var token_secret = "";
            var token = "";
            var screen_name = "";
            var user_id = "";

            for(var i=0; i<datas.length; i++){
                data = datas[i];
                if(data.indexOf("oauth_token_secret") === 0){
                    items = data.split("=");
                    token_secret = items[1];
                }else if(data.indexOf("oauth_token") === 0){
                    items = data.split("=");
                    token = items[1];
                }else if(data.indexOf("screen_name") === 0){
                    items = data.split("=");
                    screen_name = items[1];
                }else if(data.indexOf("user_id") === 0){
                    items = data.split("=");
                    user_id = items[1];
                }else{
                }
                items = null;
            }
        }

        //UIへのコールバック
        responseAccessToken(token, token_secret
                , screen_name, user_id);
    }

}
